(function($){
  // IUPAC nucleotide code
  var IUPAC=new Object();
  /* for another purpose
   *  IUPAC.A='[ARWMDHVN]';
   *  IUPAC.T='[TYWKBDHN]';
   *  IUPAC.C='[CYSMBHVN]';
   *  IUPAC.G='[GRSKBDVN]';
   *  IUPAC.R='[AGR]';
   *  IUPAC.Y='[CTY]';
   *  IUPAC.S='[GCS]';
   *  IUPAC.W='[ATW]';
   *  IUPAC.K='[GTK]';
   *  IUPAC.M='[ACM]';
   *  IUPAC.B='[CGTB]';
   *  IUPAC.D='[AGTD]';
   *  IUPAC.H='[CATH]';
   *  IUPAC.V='[CGAV]';
   *  IUPAC.N='[TCGARYSWKMBDHVN]';
  */
  IUPAC.A='A';
  IUPAC.T='T';
  IUPAC.C='C';
  IUPAC.G='G';
  IUPAC.R='[AGR]';
  IUPAC.Y='[CTY]';
  IUPAC.S='[GCS]';
  IUPAC.W='[ATW]';
  IUPAC.K='[GTK]';
  IUPAC.M='[ACM]';
  IUPAC.B='[CGTYSKB]';
  IUPAC.D='[AGTRWKD]';
  IUPAC.H='[CATMYWH]';
  IUPAC.V='[CGASMRV]';
  IUPAC.N='[TCGARYSWKMBDHVN]';

  // compliment rulers in DNA
  var comObj={};
  comObj.A='T';
  comObj.T='A';
  comObj.C='G';
  comObj.G='C';
  comObj.R='Y';
  comObj.Y='R';
  comObj.K='M';
  comObj.M='K';
  comObj.B='V';
  comObj.V='B';
  comObj.D='H';
  comObj.H='D';

  //var REIUPAC=new RegExp('[^TCGARYSWKMBDHVN]','i');
  var reiupacStr='TCGARYSWKMBDHVN';
  /*lry in 2009.01.10
   * function:get the compliment sequence of given DNA sequence
   *input:DNA sequence as string
   *return:compliment DNA sequence of input
   */
  var getCompliment=function(text){
    if(!text) return '';
  
    var p=text.toUpperCase().split('');
    var r='';
    for(var i=p.length-1;i>=0;i--){
      r += comObj[p[i]] ? comObj[p[i]] : p[i];
    }
    return r;
  }

  /*lry in 2009.01.08
   * function:check if the code of given DNA sequence is IUPAC compatibility 
   *          or if exist code in DNA sequence is IUPAC uncompatibility 
   * input: DNA sequence as string
   * output:boolen(1 for IUPAC compatibility, 0 for not)
   */
 var isIUPAC=function(text){
   var REIUPAC=new RegExp(reiupacStr,'ig');
   return !REIUPAC.test(text);
 }

 /*lry in 2009.01.10
  *function:get the analysis model and the dependecy models include in the js file of the url
  *input:url js file with the structure as like:
  *    (function(){
  *       files={'successfull':true,url:{'1':'file1','2':'file2','3':'file3'....}}
  *    })(files) 
  * 
  * return boolen(1 for get the file successfully,0 cann't get the full file)
  * todo:maybe need to follow some usefull models from jquery-plugin or dojo-require system
  * todo:if the js file is load,detect load error and js excecute error
  */
 var getModel=function(url){
   $.ajaxSetup({'async':false});
   $.getScript(url);
   if($.jbiotool.urlfiles.successfull){
     for(var i=0;i<$.jbiotool.urlfiles.url.length;i++){
       $.jbiotool.model_load_state=true;
       if($.jbiotool.urlfiles.url[i][1] == 'js'){
         $.getScript($.jbiotool.urlfiles.url[i][0]); 
       }else{
         $('head').append('<link rel="stylesheet" type="text/css" href="' + $.jbiotool.urlfiles.url[i][0] + '" />');
       }
       if($.jbiotool.model_load_state==false){
         alert($.jbiotool.urlfiles.url[i] + ':load fait');
	 $.jbiotool.model_load_state=true;
         $.ajaxSetup({'async':true});
	 return false;
       }
     }
   }else{
     alert('false');
   }
   $.jbiotool.urlfiles={};
   $.ajaxSetup({'async':true});
 }
 
 /*lry in 2010-01-18
  *delete any un IUPAC letters
  *input rna string
  *output rna string with valid letters
  */

 var filterNoDNA=function(str){
    var REIUPAC=new RegExp('[^' + reiupacStr + ']','ig');
    return str.replace(REIUPAC,'');
 }

 /*lry in 2010-01-18
  * ajax error status setting
  * input identitied to ajax.setError 
  * outpu set the global variable to false
  */ 

 var setgetModelErr=function(request,settings){
   $.jbiotool.model_load_state=false;
 }
 $.jbiotool={
   'IUPAC':IUPAC,
   'isIUPAC':isIUPAC,
   'getCompliment':getCompliment,
   'getModel':getModel,
   'urlfiles':{},
   'model_load_state':true,
   'setgetModelErr':setgetModelErr,
   'filterNoDNA':filterNoDNA
 }
})(jQuery);
